package com.yc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.vo.Result;
import com.yc.entity.LabelDept;
import com.yc.mapper.LabelDeptMapper;
import com.yc.service.LabelDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "所属部门")
@RestController
@RequestMapping("/api/labelDept")
public class LabelDeptController {

    @Autowired
    private LabelDeptService labelDeptService;
    @Autowired
    private LabelDeptMapper labelDeptMapper;


    @PostMapping
    public Result<?> save(@RequestBody LabelDept labelDept) {
        return Result.success(labelDeptService.save(labelDept));
    }


    @GetMapping("/del/{id}")
    public Result<?> delete(@PathVariable Long id) {
        labelDeptService.removeById(id);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result<LabelDept> findById(@PathVariable Long id) {
        return Result.success(labelDeptService.getById(id));
    }


    @GetMapping
    public Result<List<LabelDept>> findAll() {
        return Result.success(labelDeptService.list());
    }


    @GetMapping("/page")
    public Result<IPage<LabelDept>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                             @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(labelDeptService.page(new Page<>(pageNum, pageSize), Wrappers.<LabelDept>lambdaQuery().like(LabelDept::getTname, name)));
    }

    @ApiOperation(value = "按A-Z展示认定部门", httpMethod = "POST")
    @RequestMapping("showName")
    public Result showName(){
        try{
            List<LabelDept> list = labelDeptMapper.list();
            return Result.success(list);
        }catch (Exception e){
            return Result.error("查询部门信息失败!");
        }
    }

    @ApiOperation(value = "按倒序展示认定部门", httpMethod = "POST")
    @RequestMapping("showLabelDept")
    public Result showLabelDept(){
        try{
            List<LabelDept> list = labelDeptMapper.showAll();
            return Result.success(list);
        }catch (Exception e){
            return Result.error("查询部门信息失败!");
        }
    }


    @ApiOperation(value = "新增项目部门", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "tname", value = "新增项目认定部门名称", paramType = "String", required = true)})
    @RequestMapping("addLabelDept")
    public Result addLabelDept(String tname){
        LabelDept labelDept=new LabelDept();
        labelDept.setTname(tname);
        int result = labelDeptMapper.insert(labelDept);
        if(result<=0){
            return Result.error("新增部门失败!");
        }
        return Result.success("新增部门成功!");
    }


    @ApiOperation(value = "修改部门", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "tid", value = "修改认定部门的部门id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "tname", value = "修改认定部门的部门名称", paramType = "String", required = true)})
    @RequestMapping("updataLabelDept")
    public Result updataLabelDept(Long tid, String tname){
        LabelDept labelDept= new LabelDept();
        labelDept.setTid(tid);
        labelDept.setTname(tname);
        int result=labelDeptMapper.updateById(labelDept);
        if(result<=0){
            return Result.error("修改失败!");
        }
        return Result.success("修改成功!");
    }


    @ApiOperation(value = "删除部门", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "tid", value = "删除认定部门的id", paramType = "Long", required = true)})
    @RequestMapping("deleteLabelDept")
    public Result deleteLabelDept(Long tid){
        int result=labelDeptMapper.deleteById(tid);
        if(result<=0){
            return Result.error("删除失败!");
        }
        return Result.success("删除成功!");
    }

}
